﻿/// Write a program that removes from a text file all words listed in given another text file.
/// Handle all possible exceptions in your methods.



// Regex.Matches(str, ">(.*?)<")
using System;
using System.IO;
using System.Text.RegularExpressions;
using System.Security;


namespace c_sh_II_lek_6_files
{
    class zad12
    {
        static void Main()
        {
            try
            {
                string input = @"..\..\input.xml";
                string output = @"..\..\output.txt";
                string replace = @"..\..\replaceList.txt";
                /// tank you http://forums.academy.telerik.com/user/jasssonpet 
                string regex = @"\b(" + String.Join("|", File.ReadAllLines(replace)) + @")\b";

                using (StreamReader _in = new StreamReader(input))
                {
                    using (StreamWriter _out = new StreamWriter(output))
                    {
                        string line = _in.ReadLine();
                        while (line != null)
                        {
                            _out.WriteLine(Regex.Replace(line, regex, String.Empty));
                            line = _in.ReadLine();
                        }
                    }

                }

                File.Delete(input);
                File.Copy(output, input);
                File.Delete(output);
            }

            catch (FileNotFoundException e)
        {
            Console.WriteLine(e.Message);
        }

        catch (DirectoryNotFoundException e)
        {
            Console.WriteLine(e.Message);
        }

        catch (IOException e)
        {
            Console.WriteLine(e.Message);
        }

        catch (SecurityException e)
        {
            Console.WriteLine(e.Message);
        }

        catch (UnauthorizedAccessException e)
        {
            Console.WriteLine(e.Message);
        }
        }
    }
}
